package mapEditor.view.assetEdit
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import loader.LoaderBase;
	import loader.LoaderEvent;
	import loader.LoaderFactory;
	import loader.LoaderFlagType;
	import loader.LoaderQueue;
	import loader.LoaderType;
	
	import mapEditor.MapEditorManager;
	import mapEditor.data.AssetTemplateInfo;
	import mapEditor.data.ConfigTemplateInfo;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.AssetGroupVO;
	import mapEditor.vo.AssetVO;
	
	import utils.DisplayUtil;
	import utils.StringUtil;

	public class AssetListView extends Sprite
	{
		private var _assetLibLoaderQueue:LoaderQueue;
		private var _assetItemList:Vector.<AssetItemView>=new Vector.<AssetItemView>();
		private var _editAssetItemView:AssetItemView;
		private var _selectAssetItemView:AssetItemView;
		
		public function AssetListView()
		{
			AssetTemplateInfo.instance.addEventListener(MapEditorEvent.ASSET_GROUP_CHANGE, __onAssetGroupChange);
			loadLibAsset();
		}
		
		/**
		 *载入库中的资源项 
		 */		
		private function loadLibAsset():void
		{
			clearAssetItemList();
			_assetLibLoaderQueue=new LoaderQueue();
			_assetLibLoaderQueue.addEventListener(LoaderEvent.COMPLETE_QUEUE, __onAssetLibLoaderQueueComplete);
			for each(var assetGroupVO:AssetGroupVO in AssetTemplateInfo.instance.groups)
			{
				var path:String=assetGroupVO.path
				var load:LoaderBase=LoaderFactory.Instance.createLoader(LoaderType.LOADER, ConfigTemplateInfo.instance.mapVO.mapFileDir+"\\"+StringUtil.replace(path, "/","\\"), LoaderFlagType.COMMON, false, false);
				load.transferData=assetGroupVO;
				load.addEventListener(LoaderEvent.COMPLETE, __onAssetGroupLoadComplete);
				_assetLibLoaderQueue.addLoader(load);	
			}
			_assetLibLoaderQueue.execute();
		}
		
		private function __onAssetGroupLoadComplete(evt:LoaderEvent):void
		{
			evt.baseLoader.removeEventListener(LoaderEvent.COMPLETE, __onAssetGroupLoadComplete);
			var assetGroupVO:AssetGroupVO=evt.baseLoader.transferData as AssetGroupVO;
			createAssetItem(assetGroupVO);
			evt.baseLoader.dispose();
			evt.baseLoader=null;
		}
		
		private function createAssetItem(assetGroupVO:AssetGroupVO):void
		{
			for each(var assetVO:AssetVO in assetGroupVO.items)
			{
				var assetItem:AssetItemView=new AssetItemView(1, assetVO);
				assetItem.addEventListener(MapEditorEvent.EDITING, __onAssetItemViewEdit);
				assetItem.addEventListener(MapEditorEvent.SELECTED, __onAssetItemViewSelect);
				assetItem.x=0;
				assetItem.y=_assetItemList.length*assetItem.height+_assetItemList.length*5;
				addChild(assetItem);
				_assetItemList.push(assetItem);
			}
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function __onAssetLibLoaderQueueComplete(evt:LoaderEvent):void
		{
			_assetLibLoaderQueue.removeEventListener(LoaderEvent.COMPLETE_QUEUE, __onAssetLibLoaderQueueComplete);
			_assetLibLoaderQueue.dispose();
			_assetLibLoaderQueue=null;
		}
		
		private function clearAssetItemList():void
		{
			while(_assetItemList.length>0)
			{
				var assetItemView:AssetItemView=_assetItemList.shift();
				assetItemView.removeEventListener(MapEditorEvent.EDITING, __onAssetItemViewEdit);
				assetItemView.removeEventListener(MapEditorEvent.SELECTED, __onAssetItemViewSelect);
				DisplayUtil.dispose(assetItemView);
				assetItemView=null;
			}
		}
		
		private function __onAssetItemViewSelect(evt:MapEditorEvent):void
		{
			if(_selectAssetItemView)_selectAssetItemView.isSelected=false;
			_selectAssetItemView=evt.currentTarget as AssetItemView;
			dispatchEvent(new Event(Event.SELECT));
		}
		
		private function __onAssetItemViewEdit(evt:MapEditorEvent):void
		{
			if(_editAssetItemView)_editAssetItemView.isEdit=false;
			_editAssetItemView=evt.target as AssetItemView;
			MapEditorManager.instance.selectAssetItem(_editAssetItemView.assetVO);
		}
		
		private function __onAssetGroupChange(evt:MapEditorEvent):void
		{
			loadLibAsset();
		}
		
		public function dispose():void
		{
			AssetTemplateInfo.instance.removeEventListener(MapEditorEvent.ASSET_GROUP_CHANGE, __onAssetGroupChange);
			
			if(_assetLibLoaderQueue)
			{
				_assetLibLoaderQueue.removeEventListener(LoaderEvent.COMPLETE_QUEUE, __onAssetLibLoaderQueueComplete);
				_assetLibLoaderQueue.dispose();
				_assetLibLoaderQueue=null;
			}
			_editAssetItemView=null;
			_selectAssetItemView=null;
			clearAssetItemList();
			_assetItemList=null;
		}

		public function get selectAssetItemView():AssetItemView
		{
			return _selectAssetItemView;
		}
	}
}